<?php
namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\EngineerConfigItem;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;
use app\common\model\ViperCardConfig as ConfigModel;
use think\image\Exception;
use think\Model;
use think\Validate;


/**
 * 会员阶梯提成配置
 * @package app\index\controller
 */
class ViperCardConfig extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取会员卡阶梯提成列表
     */
    public function configList(){
        if($this->request->isPost()){
            $data = Db::name('qt_viper_card_config')
                ->alias('config')
                ->field('config.*,u.real_name')
                ->leftJoin('qt_users u','u.id = config.user_id')
                ->where(['config.store_id'=>$this->userInfo['store_id']])
                ->select();
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求','',204);


    }


    /**
     * 添加会员卡阶梯提成列表
     */
    public function configAdd(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            $rule = [
                'role|角色' => 'require',
                'type|上钟类型' => 'require',
                'money_min|最小金额' => 'require|number',
                'money_max|最大金额' => 'require|number',
                'rate|提成比例' => 'require',
            ];
            // 定义验证提示
            $message = [
                'role.require' => '请选择角色',
                'type.require' => '请选择提成类型',
                'clock_type.require' => '请选择上钟类型',
                'money_min.require' => '请填写金额范围',
                'money_max.require' => '请填写金额范围',
                'money_min.number' => '金额格式不正确',
                'money_max.number' => '金额格式不正确',
                'rate.require' => '请填写提成比例',

            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['user_id'] = $this->userInfo['id'];
            $getData['create_time'] = time();
            try {
                 ConfigModel::create($getData);
            }catch (\Exception $e) {
                $this->result($e->getMessage(),'',204);
            }
            $this->result('添加成功','',200);

        }
        $this->result('无效的请求','',204);
    }



    /**
     * 编辑会员卡阶梯提成列表
     */
    public function configEdit(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            $rule = [
                'id|阶梯提成' => 'require',
                'role|角色' => 'require',
                'type|上钟类型' => 'require',
                'money_min|最小金额' => 'require|number',
                'money_max|最大金额' => 'require|number',
                'rate|提成比例' => 'require',
            ];
            // 定义验证提示
            $message = [
                'id.require' => '缺少阶梯提成ID',
                'role.require' => '请选择角色',
                'type.require' => '请选择提成类型',
                'clock_type.require' => '请选择上钟类型',
                'money_min.require' => '请填写金额范围',
                'money_max.require' => '请填写金额范围',
                'money_min.number' => '金额格式不正确',
                'money_max.number' => '金额格式不正确',
                'rate.require' => '请填写提成比例',

            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            ConfigModel::where(['id'=>$getData['id']])->update($getData);
            $this->result('添加成功','',200);
        }
        $this->result('无效的请求','',204);
    }



    /**
     * 删除会员卡阶梯提成列表
     */
    public function configDel(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            try {
                ConfigModel::where(['id'=>$getData['id']])->delete();
            }catch (\Exception $e){
                $this->result($e->getMessage(),'',204);
            }
            $this->result('删除成功','',200);

        }
        $this->result('无效的请求','',204);
    }




}
